package org.epidroid.epidroid.news.ui;

import java.util.ArrayList;

import org.apache.commons.net.nntp.NNTPClient;
import org.epidroid.epidroid.R;
import org.epidroid.epidroid.androsoul.engine.service.IAndroSoulServiceListener;
import org.epidroid.epidroid.news.engine.Group;
import org.epidroid.epidroid.news.engine.INewsService;
import org.epidroid.epidroid.news.engine.NewsService;
import org.epidroid.epidroid.news.engine.NewsServiceBinder;
import org.epidroid.epidroid.news.engine.Topic;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Activity that lists the topics inside a newsgroup
 * 
 * @author conrat_a
 * 
 */
public class ListTopicsActivity extends ListActivity {

	private INewsService _service = null;

	private Group _group = null;
	private ServiceConnection _serviceConnection = null;
	private NNTPClient _client = null;
	private ArrayList<Topic> _list = null;
	private ListTopicsAdapter _adapter = null;
	private ProgressDialog _progressDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_group_list);

		_list = new ArrayList<Topic>();
		_adapter = new ListTopicsAdapter(
				ListTopicsActivity.this,
				R.layout.news_group_item, _list);
		
		((ListView) getListView()).setAdapter(_adapter);
		
		
		showProgressDialog();
		bindWithNewsService();
	}

	@Override
	protected void onDestroy() {
		unbindService(_serviceConnection);
		hideProgressDialog();
		super.onDestroy();
	}

	private void bindWithNewsService() {
		Intent intent = new Intent(this, NewsService.class);

		final IAndroSoulServiceListener listener = new IAndroSoulServiceListener() {
			public void dataChanged(final Message data) {
				ListTopicsActivity.this.runOnUiThread(new Runnable() {
					public void run() {
						// Nothing to do here for the moment
					}
				});
			}
		};

		_serviceConnection = new ServiceConnection() {
			public void onServiceConnected(ComponentName name,
					IBinder servicebinder) {
				INewsService service = ((NewsServiceBinder) servicebinder)
						.getService();
				_service = service;
				Log.i(NewsActivity.LOGCAT, "Service is bound!");
				init();
			}

			public void onServiceDisconnected(ComponentName name) {
				Log.i(NewsActivity.LOGCAT, "Service has disconnected!");
			}
		};

		bindService(intent, _serviceConnection, Context.BIND_AUTO_CREATE);
	}

	public void init() {
		launchThreadGetTopics();
	}
	
	private void launchThreadGetTopics () {
		AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
			private String name = null;
			
			private void updateDisplay () {
				if (_list == null || _list.size() == 0)
					return;
				Log.d("News", "updateDisplay");
				_adapter.setItems(_list);
				_adapter.notifyDataSetChanged();

				TextView text = (TextView) findViewById(R.id.news_group_title);
				text.setText(name);
				
				hideProgressDialog();
			}
			
			@Override
			protected void onProgressUpdate (Void... values) {
				Log.d("News", "onProgressUpdate");
				updateDisplay ();
			}
			
			@Override
			protected void onPostExecute (Boolean result) {
				Log.d("News", "onPostExecute");
				updateDisplay();
			}

			@Override
			protected Boolean doInBackground(Void... params) {
				Intent intent = getIntent();
				name = intent.getExtras().getString("newsgroup");
				Log.v(NewsActivity.LOGCAT, "Launched " + name);
				_group = _service.getGroupFromName(name);
				if (_group == null) {
					// TODO
					return false;
				}

				_list = _service.getListTopics(_group);
				publishProgress();
				
				_list = _service.updateListTopics(_group);
				
				return true;
			}
		};
		task.execute();
	}

	public void showTopic(String topicId) {
		Intent intent = new Intent(this, TopicActivity.class);
		intent.putExtra("group", _group.getTitle());
		intent.putExtra("topic", topicId);
		startActivity(intent);
	}

	private void showProgressDialog() {
		if (_progressDialog != null)
			return;
		_progressDialog = ProgressDialog
				.show(this, "", "Retrieving data", true);
		_progressDialog.setCancelable(true);
		_progressDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO
			}

		});
	}

	private void hideProgressDialog() {
		if (_progressDialog != null) {
			_progressDialog.dismiss();
			_progressDialog = null;
		}
	}

}
